import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt  # 绘制图像的模块
import json
from os import path

f = open(path.dirname(__file__)+'/douban.json', 'r', encoding='utf-8')
jsonInfo = json.load(f)
comments = ''
for item in jsonInfo:
    comments += item['comment']+' '

# 结巴分词，生成字符串，wordcloud无法直接生成正确的中文词云
cut_text = " ".join(jieba.cut(comments))

print(cut_text)
wordcloud = WordCloud(
    # 设置字体，不然会出现口字乱码，文字的路径是电脑的字体一般路径，可以换成别的
    font_path="/System/Library/Fonts/Hiragino Sans GB.ttc", # mac字体
    # 设置了背景，宽高
    background_color="white", 
    width=1000, 
    height=880).generate(cut_text)

plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
